﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BulletinModel;
using System.Web.Mvc;
using BulletinMvc.Models;
using System.Data.Entity;
using System.Data;
namespace BulletinMvc.Lib
{
      
    public class BulletinBL:IBulletinBL
    {
        private BulletinDBEntities db;
        private ModelStateDictionary _errorLog;

        public BulletinBL(ModelStateDictionary modelState)
        {
            _errorLog = modelState;
            db = new BulletinDBEntities();
        }
        public IQueryable<Bulletin> GetBulletinList()
        {

            return db.Bulletins.OrderByDescending(a => a.ID);
        }
        public IQueryable<Bulletin> GetAnonymousBulletinList()
        {

            return db.Bulletins.Where(b=>b.Visible==true&&b.StartDate<DateTime.Now&&b.DueDate>DateTime.Now).OrderByDescending(a => a.ID);      
        }
        

        public bool DeleteBulletin(int id)
        {
            if (db.BulletinItems.Where(b => b.BulletinID == id).Count() > 0)
            {
                _errorLog.AddModelError("", "该项目已经有子项目!请先删除子项目!");
                return false;
            }
            else
            {
                Bulletin bulletin = db.Bulletins.Where(a => a.ID == id).Single();
                db.Bulletins.Remove(bulletin);
                db.SaveChanges();
                return true;
            }


              
        }

        public void saveBulletin(BulletinCreateModel savebulletin)
        {
            Bulletin bulletin = new Bulletin
            {
                Name = savebulletin.name,
                StartDate = savebulletin.startdatetime,
                DueDate = savebulletin.Duedatetime,
                StarFeedbackEnabled = savebulletin.StarFeedbackEnabled,
                TextFeedbackEnabled = savebulletin.TextFeedbackEnabled,
                Visible = savebulletin.visible,
                Description = savebulletin.Description

            };
                db.Bulletins.Add(bulletin);
                db.SaveChanges();
        }
      //public void comparedatetime( DateTime startime ,DateTime duetime)
      //{

      //    if (startime <= duetime) { 
          
          
      //    }
      
      
      //}

        public void SaveEditBulletin(int BulletinID,  BulletinCreateModel savebulletin)
        {
            Bulletin bulletin = new Bulletin
            {
                 ID=BulletinID,
                  Name=savebulletin.name,
                   StartDate=savebulletin.startdatetime,
                    DueDate=savebulletin.Duedatetime,
                     StarFeedbackEnabled=savebulletin.StarFeedbackEnabled,
                      TextFeedbackEnabled=savebulletin.TextFeedbackEnabled,
                       Visible=savebulletin.visible,
                        Description=savebulletin.Description
                        
                         
            };
            db.Bulletins.Attach(bulletin);
            db.Entry(bulletin).State = EntityState.Modified;
            db.SaveChanges();

        }
        public int GetBulletinCount() 
        {
            return db.Bulletins.Count();
        
        
        }
      public  BulletinCreateModel DeleteDetail(int id) 
        {
            var bulletin = from a in db.Bulletins
                           where a.ID == id
                           select new BulletinCreateModel
                           {
                               name = a.Name,
                               startdatetime = a.StartDate,
                               Duedatetime = a.DueDate,
                               StarFeedbackEnabled = a.StarFeedbackEnabled,
                               TextFeedbackEnabled = a.TextFeedbackEnabled,
                               Description = a.Description,
                               visible = a.Visible
                           };
            return bulletin.SingleOrDefault();
        
        }
     public    String GetBulletinName(int BulletinId) 
      {
          return db.Bulletins.Where(a => a.ID == BulletinId).SingleOrDefault().Name.ToString();
      }
      public   String GetBulletinDescription(int BulletinId) 
      {
         // return db.Bulletins.Where(a => a.ID == BulletinId).SingleOrDefault().Description.ToString();
          var description = (from a in db.Bulletins where a.ID == BulletinId select a.Description).SingleOrDefault();
          if (description == null)
              return "";
          else return description.ToString();
      
      }



    }
}